package com.longcai.piaojia.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.longcai.piaojia.R;
import com.longcai.piaojia.conn.ProblemsListGet;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.List;

/**
 * Created by Administrator on 2016/11/4.
 */
public class ProblemsAdapter extends BaseExpandableListAdapter {

    private LayoutInflater layoutInflater;
    private Context context;
    private List<ProblemsListGet.ProblemEntity> childList;

    public ProblemsAdapter(Context context, List<ProblemsListGet.ProblemEntity> childList) {

        layoutInflater = LayoutInflater.from(context);

        this.childList = childList;

        this.context = context;
    }

    @Override
    public int getGroupCount() {
        return childList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childList.get(groupPosition).problemItemEntities.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return childList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childList.get(groupPosition).problemItemEntities.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        if (convertView == null) {

            convertView = ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) layoutInflater.inflate(R.layout.item_problems_group, null));

        }

        TextView item_problems_group_tv = (TextView) convertView.findViewById(R.id.item_problems_group_tv);
        ImageView problems_group_iv = (ImageView) convertView.findViewById(R.id.problems_group_iv);

        String title = childList.get(groupPosition).title;

        item_problems_group_tv.setText(title);

        if (isExpanded) {

            problems_group_iv.setImageResource(R.mipmap.expandablelistview_up);

        } else {

            problems_group_iv.setImageResource(R.mipmap.expandaablelistview_down);

        }

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {


        if (convertView == null) {

            convertView = ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) layoutInflater.inflate(R.layout.item_problems_child, null));

        }

        TextView problems_child_question_tv = (TextView) convertView.findViewById(R.id.problems_child_question_tv);
        TextView problems_child_answer_tv = (TextView) convertView.findViewById(R.id.problems_child_answer_tv);

        problems_child_question_tv.setText(childList.get(groupPosition).problemItemEntities.get(childPosition).title);
        problems_child_answer_tv.setText(childList.get(groupPosition).problemItemEntities.get(childPosition).answer);

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
}
